package com.masterclient.app.modules.shop.adapter;

import android.app.Activity;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.android.vlayout.DelegateAdapter;
import com.alibaba.android.vlayout.LayoutHelper;
import com.alibaba.android.vlayout.VirtualLayoutManager;
import com.masterclient.app.R;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @auther hjian
 * @date 2020/4/9.
 */
public class StoreSequenceLayoutAdapter extends DelegateAdapter.Adapter<StoreSequenceLayoutAdapter.ViewHolder> {
    private Activity mContext;
    private LayoutHelper mLayoutHelper;
    private VirtualLayoutManager.LayoutParams mLayoutParams;
    private int count = 0;
    private String sequenceName = "sale";
    private boolean isSwitchGrid = true;
    private StoreSequenceLayoutAdapter.ViewHolder holder;
    private boolean showCommission;

    private SequenceClickListener sequenceClickListener;

    public interface SequenceClickListener {
        /**
         * @param sequenceName 排序名称
         */
        void onSequenceClick(String sequenceName);

        void onSwitchList(boolean isSwitchGrid);
    }

    public StoreSequenceLayoutAdapter(Activity context, LayoutHelper layoutHelper, int count, boolean showCommission, @NonNull VirtualLayoutManager.LayoutParams layoutParams, SequenceClickListener sequenceClickListener) {
        this.mContext = context;
        this.mLayoutHelper = layoutHelper;
        this.count = count;
        this.mLayoutParams = layoutParams;
        this.sequenceClickListener = sequenceClickListener;
        this.showCommission = showCommission;
    }

    @Override
    public LayoutHelper onCreateLayoutHelper() {
        return mLayoutHelper;
    }

    @NonNull
    @Override
    public StoreSequenceLayoutAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new StoreSequenceLayoutAdapter.ViewHolder(LayoutInflater.from(mContext).inflate(R.layout.store_sequence, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull StoreSequenceLayoutAdapter.ViewHolder holder, int position) {
        if (mLayoutParams != null) {
            holder.itemView.setLayoutParams(
                    new VirtualLayoutManager.LayoutParams(mLayoutParams));
        }
        this.holder = holder;
            holder.storeSequenceCommission.setVisibility(!showCommission?View.GONE:View.VISIBLE);
            holder.storeSequenceSale.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if ("hot".equals(sequenceName)) return;
                sequenceName = "hot";
                setSequence(holder);
                sequenceClickListener.onSequenceClick(sequenceName);
            }
        });
        holder.storeSequenceCommission.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if ("commission".equals(sequenceName)) return;
                sequenceName = "commission";
                setSequence(holder);
                sequenceClickListener.onSequenceClick(sequenceName);
            }
        });
        holder.storeSequenceNew.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if ("new".equals(sequenceName)) return;
                sequenceName = "new";
                setSequence(holder);
                sequenceClickListener.onSequenceClick(sequenceName);
            }
        });

        holder.storeSequencePricell.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                sequenceName = sequenceName.equals("price_asc") ? "price_desc" : "price_asc";
                setSequence(holder);
                sequenceClickListener.onSequenceClick(sequenceName);
            }
        });
        holder.storeSequenceSwitch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                isSwitchGrid = !isSwitchGrid;
                holder.storeSequenceSwitchicon.setImageResource(isSwitchGrid ? R.mipmap.store_switch_grid : R.mipmap.store_switch_list);
                sequenceClickListener.onSwitchList(isSwitchGrid);
            }
        });
        holder.storeSequenceCommission.setVisibility(!showCommission ? View.GONE : View.VISIBLE);
    }

    public void refresh(){
        sequenceName = "hot";
        setSequence(holder);
    }


    @Override
    public int getItemCount() {
        return count;
    }

    @Override
    public int getItemViewType(int position) {
        return 4;
    }

    private void setSequence(StoreSequenceLayoutAdapter.ViewHolder holder) {
        holder.storeSequenceSale.setTextColor(0xFF141922);
        holder.storeSequenceCommission.setTextColor(0xFF141922);
        holder.storeSequenceNew.setTextColor(0xFF141922);
        holder.storeSequencePrice.setTextColor(0xFF141922);
        holder.storeSequencePriceicon.setImageResource(R.mipmap.paixu_icon);
        switch (sequenceName) {
            case "hot":
                holder.storeSequenceSale.setTextColor(0xFFF23051);
                break;
            case "commission":
                holder.storeSequenceCommission.setTextColor(0xFFF23051);
                break;
            case "new":
                holder.storeSequenceNew.setTextColor(0xFFF23051);
                break;
            case "price_asc":
                holder.storeSequencePrice.setTextColor(0xFFF23051);
                holder.storeSequencePriceicon.setImageResource(R.mipmap.paixu_top_icon);
                break;
            case "price_desc":
                holder.storeSequencePrice.setTextColor(0xFFF23051);
                holder.storeSequencePriceicon.setImageResource(R.mipmap.paixu_down_icon);
                break;
            default:
        }
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.store_sequence_sale)
        TextView storeSequenceSale;
        @BindView(R.id.store_sequence_commission)
        TextView storeSequenceCommission;
        @BindView(R.id.store_sequence_new)
        TextView storeSequenceNew;
        @BindView(R.id.store_sequence_price)
        TextView storeSequencePrice;
        @BindView(R.id.store_sequence_priceicon)
        ImageView storeSequencePriceicon;
        @BindView(R.id.store_sequence_pricell)
        LinearLayout storeSequencePricell;
        @BindView(R.id.store_sequence_switch)
        FrameLayout storeSequenceSwitch;
        @BindView(R.id.store_sequence_switchicon)
        ImageView storeSequenceSwitchicon;

        ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }
}
